package cn.rongcloud.im.server;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;


import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.rongcloud.im.server.network.http.HttpException;
import cn.rongcloud.im.server.network.http.RequestParams;
import cn.rongcloud.im.server.request.AddGroupMemberRequest;
import cn.rongcloud.im.server.request.AddToBlackListRequest;
import cn.rongcloud.im.server.request.AgreeFriendsRequest;
import cn.rongcloud.im.server.request.ChangePasswordRequest;
import cn.rongcloud.im.server.request.CheckPhoneRequest;
import cn.rongcloud.im.server.request.CreateGroupRequest;
import cn.rongcloud.im.server.request.DeleteFriendRequest;
import cn.rongcloud.im.server.request.DeleteGroupMemberRequest;
import cn.rongcloud.im.server.request.DismissGroupRequest;
import cn.rongcloud.im.server.request.FriendInvitationRequest;
import cn.rongcloud.im.server.request.JoinGroupRequest;
import cn.rongcloud.im.server.request.LoginRequest;
import cn.rongcloud.im.server.request.QuitGroupRequest;
import cn.rongcloud.im.server.request.RegisterRequest;
import cn.rongcloud.im.server.request.RemoveFromBlacklistRequest;
import cn.rongcloud.im.server.request.RestPasswordRequest;
import cn.rongcloud.im.server.request.SendCodeRequest;
import cn.rongcloud.im.server.request.SetFriendDisplayNameRequest;
import cn.rongcloud.im.server.request.SetGroupDisplayNameRequest;
import cn.rongcloud.im.server.request.SetGroupNameRequest;
import cn.rongcloud.im.server.request.SetGroupPortraitRequest;
import cn.rongcloud.im.server.request.SetNameRequest;
import cn.rongcloud.im.server.request.SetPortraitRequest;
import cn.rongcloud.im.server.request.VerifyCodeRequest;
import cn.rongcloud.im.server.request.base.BaseRequest;
import cn.rongcloud.im.server.response.AddGroupMemberResponse;
import cn.rongcloud.im.server.response.AddToBlackListResponse;
import cn.rongcloud.im.server.response.AgreeFriendsResponse;
import cn.rongcloud.im.server.response.ChangePasswordResponse;
import cn.rongcloud.im.server.response.CheckPhoneResponse;
import cn.rongcloud.im.server.response.CoinCountByUserResponse;
import cn.rongcloud.im.server.response.CoinListResponse;
import cn.rongcloud.im.server.response.CreateGroupResponse;
import cn.rongcloud.im.server.response.DefaultConversationResponse;
import cn.rongcloud.im.server.response.DeleteFriendResponse;
import cn.rongcloud.im.server.response.DeleteGroupMemberResponse;
import cn.rongcloud.im.server.response.DismissGroupResponse;
import cn.rongcloud.im.server.response.FriendInvitationResponse;
import cn.rongcloud.im.server.response.GetBlackListResponse;
import cn.rongcloud.im.server.response.GetFriendInfoByIDResponse;
import cn.rongcloud.im.server.response.GetGroupInfoResponse;
import cn.rongcloud.im.server.response.GetGroupMemberResponse;
import cn.rongcloud.im.server.response.GetGroupResponse;
import cn.rongcloud.im.server.response.GetTokenResponse;
import cn.rongcloud.im.server.response.GetUserInfoByIdResponse;
import cn.rongcloud.im.server.response.GetUserInfoByPhoneResponse;
import cn.rongcloud.im.server.response.GetUserInfosResponse;
import cn.rongcloud.im.server.response.JoinGroupResponse;
import cn.rongcloud.im.server.response.LoginResponse;
import cn.rongcloud.im.server.response.MachineUserListResponse;
import cn.rongcloud.im.server.response.QiNiuTokenResponse;
import cn.rongcloud.im.server.response.QuitGroupResponse;
import cn.rongcloud.im.server.response.RegisterResponse;
import cn.rongcloud.im.server.response.RemoveFromBlackListResponse;
import cn.rongcloud.im.server.response.RestPasswordResponse;
import cn.rongcloud.im.server.response.SellMachineListResponse;
import cn.rongcloud.im.server.response.SendCodeResponse;
import cn.rongcloud.im.server.response.SetFriendDisplayNameResponse;
import cn.rongcloud.im.server.response.SetGroupDisplayNameResponse;
import cn.rongcloud.im.server.response.SetGroupPortraitResponse;
import cn.rongcloud.im.server.response.SetNameResponse;
import cn.rongcloud.im.server.response.SetPortraitResponse;
import cn.rongcloud.im.server.response.SyncTotalDataResponse;
import cn.rongcloud.im.server.response.UserInfoResponse;
import cn.rongcloud.im.server.response.UserRelationshipResponse;
import cn.rongcloud.im.server.response.VerifyCodeResponse;
import cn.rongcloud.im.server.response.SetGroupNameResponse;
import cn.rongcloud.im.server.response.VersionResponse;
import cn.rongcloud.im.server.response.friendcircle.MomentsInfo;
import cn.rongcloud.im.server.utils.NLog;
import cn.rongcloud.im.server.utils.json.JsonMananger;

/**
 * Created by AMing on 16/1/14.
 * Company RongCloud
 */
@SuppressWarnings("deprecation")
public class SealAction extends BaseAction {
    private final String CONTENT_TYPE = "application/json";
    private final String ENCODING = "utf-8";

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public SealAction(Context context) {
        super(context);
    }


    /**
     * 处理请求entity
     *
     * @param baseRequest
     * @return
     */
    private UrlEncodedFormEntity getRequestEntity(BaseRequest baseRequest) {
        UrlEncodedFormEntity entity1 = null;
        try {
            Map<String, String> mapParams = baseRequest.getMapParams();

            List<NameValuePair> list = new ArrayList<NameValuePair>();
            if (mapParams != null && !mapParams.isEmpty()) {
                for (Map.Entry<String, String> entry : mapParams.entrySet()) {
                    // 解析Map传递的参数，使用一个键值对对象BasicNameValuePair保存。
                    list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
            }
            // 实现将请求 的参数封装封装到HttpEntity中。
            entity1 = new UrlEncodedFormEntity(list, ENCODING);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return entity1;
    }

    /**
     * 检查手机是否被注册
     *
     * @param region 国家码
     * @param phone  手机号
     * @throws HttpException
     */
    public CheckPhoneResponse checkPhoneAvailable(String region, String phone) throws HttpException {
        String url = getURL("user/check_phone_available");
        String json = JsonMananger.beanToJson(new CheckPhoneRequest(phone, region));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        CheckPhoneResponse response = null;
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, CheckPhoneResponse.class);
        }
        return response;
    }


    /**
     * 发送验证码
     *
     * @param region 国家码
     * @param phone  手机号
     * @throws HttpException
     */
    public SendCodeResponse sendCode(String region, String phone) throws HttpException {
        String url = getURL("register/get-check-code");
        String json = JsonMananger.beanToJson(new SendCodeRequest(region, phone, 1, true));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        SendCodeResponse response = null;
        UrlEncodedFormEntity requestEntity = getRequestEntity(new SendCodeRequest(region, phone, 1, true));

//            String result = httpManager.post(mContext, url, entity, CONTENT_TYPE);
        String result = httpManager.post(mContext, url, requestEntity, null, null);
        if (!TextUtils.isEmpty(result)) {
            response = JsonMananger.jsonToBean(result, SendCodeResponse.class);
        }
        return response;
    }

    /*
    * 200: 验证成功
    1000: 验证码错误
    2000: 验证码过期
    异常返回，返回的 HTTP Status Code 如下：

    400: 错误的请求
    500: 应用服务器内部错误
    * */

    /**
     * 验证验证码是否正确(必选先用手机号码调sendcode)
     *
     * @param region 国家码
     * @param phone  手机号
     * @throws HttpException
     */
    public VerifyCodeResponse verifyCode(String region, String phone, String code) throws HttpException {
        String url = getURL("user/verify_code");
        String json = JsonMananger.beanToJson(new VerifyCodeRequest(region, phone, code));
        VerifyCodeResponse response = null;
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        if (!TextUtils.isEmpty(result)) {
            Log.e("VerifyCodeResponse", result);
            response = jsonToBean(result, VerifyCodeResponse.class);
        }
        return response;
    }

    /**
     * 注册
     *
     * @param phoneNum 手机号码
     * @param password 密码
     * @param passsure 确认密码
     * @param vcode    验证码
     * @throws HttpException
     */
    public RegisterResponse register(String phoneNum, String password, String passsure, String vcode) throws HttpException {
        String url = getURL("register/user-register");
        StringEntity entity = null;
        try {
            entity = new StringEntity(JsonMananger.beanToJson(new RegisterRequest(phoneNum, password, passsure, vcode)), ENCODING);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        RegisterResponse response = null;
        UrlEncodedFormEntity requestEntity = getRequestEntity(new RegisterRequest(phoneNum, password, passsure, vcode));
        String result = httpManager.post(mContext, url, requestEntity, null, null);
        if (!TextUtils.isEmpty(result)) {
            NLog.e("RegisterResponse", result);
            response = jsonToBean(result, RegisterResponse.class);
        }
        return response;
    }

    /**
     * 登录: 登录成功后，会设置 Cookie，后续接口调用需要登录的权限都依赖于 Cookie。
     *
     * @param region   国家码
     * @param phone    手机号
     * @param password 密码
     * @throws HttpException
     */
    public LoginResponse login(String region, String phone, String password) throws HttpException {
        String uri = getURL("login/user-login");
        String json = JsonMananger.beanToJson(new LoginRequest(region, phone, password, ""));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        UrlEncodedFormEntity requestEntity = getRequestEntity(new LoginRequest(region, phone, password, "/user/index"));
        String result = httpManager.post(mContext, uri, requestEntity, null, null);
        LoginResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            NLog.e("LoginResponse", result);
            response = JsonMananger.jsonToBean(result, LoginResponse.class);
        }
        return response;
    }


    /**
     * 获取 token 前置条件需要登录   502 坏的网关 测试环境用户已达上限
     *
     * @throws HttpException
     */
    public GetTokenResponse getToken() throws HttpException {
        String url = getURL("user/get_token");
        String result = httpManager.get(url, mHeadToken);
        GetTokenResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            NLog.e("GetTokenResponse", result);
            response = jsonToBean(result, GetTokenResponse.class);
        }
        return response;
    }

    /**
     * 设置自己的昵称
     *
     * @param nickname 昵称
     * @throws HttpException
     */
    public SetNameResponse setName(String nickname) throws HttpException {
        String url = getURL("user/set_nickname");
        String json = JsonMananger.beanToJson(new SetNameRequest(nickname));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        SetNameResponse response = null;
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SetNameResponse.class);
        }
        return response;
    }

    /**
     * 设置用户头像
     *
     * @param portraitUri 头像 path
     * @throws HttpException
     */
    public SetPortraitResponse setPortrait(String portraitUri) throws HttpException {
        String url = getURL("user/set_portrait_uri");
        String json = JsonMananger.beanToJson(new SetPortraitRequest(portraitUri));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        SetPortraitResponse response = null;
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SetPortraitResponse.class);
        }
        return response;
    }


    /**
     * 当前登录用户通过旧密码设置新密码  前置条件需要登录才能访问
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @throws HttpException
     */
    public ChangePasswordResponse changePassword(String oldPassword, String newPassword) throws HttpException {
        String url = getURL("user/change_password");
        String json = JsonMananger.beanToJson(new ChangePasswordRequest(oldPassword, newPassword));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        ChangePasswordResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            NLog.e("ChangePasswordResponse", result);
            response = jsonToBean(result, ChangePasswordResponse.class);
        }
        return response;
    }


    /**
     * 通过手机验证码重置密码
     *
     * @param password           密码，6 到 20 个字节，不能包含空格
     * @param verification_token 调用 /user/verify_code 成功后返回的 activation_token
     * @throws HttpException
     */
    public RestPasswordResponse restPassword(String password, String verification_token) throws HttpException {
        String uri = getURL("user/reset_password");
        String json = JsonMananger.beanToJson(new RestPasswordRequest(password, verification_token));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, uri, entity, mHeadToken, CONTENT_TYPE);
        RestPasswordResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            NLog.e("RestPasswordResponse", result);
            response = jsonToBean(result, RestPasswordResponse.class);
        }
        return response;
    }

    /**
     * 根据 id 去服务端查询用户信息
     *
     * @param userid 用户ID
     * @throws HttpException
     */
    public GetUserInfoByIdResponse getUserInfoById(String userid) throws HttpException {
        String url = getURL("user/" + userid);
        String result = httpManager.get(url, mHeadToken);
        GetUserInfoByIdResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, GetUserInfoByIdResponse.class);
        }
        return response;
    }


    /**
     * 通过手机号查询用户信息
     *
     * @param region 国家码
     * @param phone  手机号
     * @throws HttpException
     */
    public GetUserInfoByPhoneResponse getUserInfoFromPhone(String region, String phone) throws HttpException {
        String url = getURL("user/findUser");
        RequestParams params = new RequestParams();
        params.put("con", phone);
        String result = httpManager.get(url, params, mHeadToken);
        GetUserInfoByPhoneResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, GetUserInfoByPhoneResponse.class);
        }
        return response;
    }


    /**
     * 发送好友邀请
     *
     * @param userid           好友id
     * @param addFriendMessage 添加好友的信息
     * @throws HttpException
     */
    public FriendInvitationResponse sendFriendInvitation(String userid, String addFriendMessage) throws HttpException {
        String url = getURL("friend/addFriend");
        String json = JsonMananger.beanToJson(new FriendInvitationRequest(userid, addFriendMessage));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        UrlEncodedFormEntity requestEntity = getRequestEntity(new FriendInvitationRequest(userid, addFriendMessage));
        String result = httpManager.post(mContext, url, requestEntity, mHeadToken, null);
        FriendInvitationResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, FriendInvitationResponse.class);
        }
        return response;
    }


    /**
     * 查看好友请求列表接口 （分页）-----------获取发生过用户关系的列表
     *
     * @throws HttpException
     */
    public UserRelationshipResponse getAllUserRelationship() throws HttpException {
        String url = getURL("friend/listFriendRequestByPage");
        RequestParams params = new RequestParams();
        params.put("pageNum", "1");
        String result = httpManager.get(url, params, mHeadToken);
        UserRelationshipResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, UserRelationshipResponse.class);
        }
        return response;
    }

    /**
     * 好友通讯录列表
     *
     * @throws HttpException
     */
    public UserRelationshipResponse getAllUserFriend() throws HttpException {
        mHeadToken = getToken(mContext);
        String url = getURL("friend/listFrient");
        String result = httpManager.get(url, mHeadToken);
        UserRelationshipResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, UserRelationshipResponse.class);
        }
        return response;
    }

    /**
     * 根据userId去服务器查询好友信息
     *
     * @throws HttpException
     */
    public GetFriendInfoByIDResponse getFriendInfoByID(String userid) throws HttpException {
        String url = getURL("friendship/" + userid + "/profile");
        String result = httpManager.get(url, mHeadToken);
        GetFriendInfoByIDResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, GetFriendInfoByIDResponse.class);
        }
        return response;
    }

    /**
     * 同意添加好友请求接口------------同意对方好友邀请
     *
     * @param friendId id=查看好友请求列表接口 （分页）返回的requestId
     * @throws HttpException
     */
    public AgreeFriendsResponse agreeFriends(String friendId) throws HttpException {
        String url = getURL("friend/addFriendSuccess");
        String json = JsonMananger.beanToJson(new AgreeFriendsRequest(friendId));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        UrlEncodedFormEntity requestEntity = getRequestEntity(new AgreeFriendsRequest(friendId));
        String result = httpManager.post(mContext, url, requestEntity, mHeadToken, null);
        AgreeFriendsResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, AgreeFriendsResponse.class);
        }
        return response;
    }



    /**
     * 创建群组
     *
     * @param name      群组名称
     * @param code      群号（融云code）
     * @param memberIds 群组成员id      uid=群成员id(数组：例 12,34,45)
     * @throws HttpException
     */
    public CreateGroupResponse createGroup(String name, String code, List<String> memberIds) throws HttpException {
        String url = getURL("chatGroup/add");
        String json = JsonMananger.beanToJson(new CreateGroupRequest(name, memberIds,code));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        CreateGroupResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, CreateGroupResponse.class);
        }
        return response;
    }

    /**
     * 创建者设置群组头像
     *
     * @param groupId     群组Id
     * @param portraitUri 群组头像
     * @throws HttpException
     */
    public SetGroupPortraitResponse setGroupPortrait(String groupId, String portraitUri) throws HttpException {
        String url = getURL("group/set_portrait_uri");
        String json = JsonMananger.beanToJson(new SetGroupPortraitRequest(groupId, portraitUri));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        SetGroupPortraitResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SetGroupPortraitResponse.class);
        }
        return response;
    }

    /**
     * 获取当前用户所属群组列表
     *
     * @throws HttpException
     */
    public GetGroupResponse getGroups() throws HttpException {
        String url = getURL("user/groups");
        String result = httpManager.get(mContext, url, mHeadToken);
        GetGroupResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, GetGroupResponse.class);
        }
        return response;
    }

    /**
     * 根据 群组id 查询该群组信息   403 群组成员才能看
     *
     * @param groupId 群组Id
     * @throws HttpException
     */
    public GetGroupInfoResponse getGroupInfo(String groupId) throws HttpException {
        String url = getURL("group/" + groupId);
        String result = httpManager.get(mContext, url, mHeadToken);
        GetGroupInfoResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, GetGroupInfoResponse.class);
        }
        return response;
    }

    /**
     * 根据群id获取群组成员
     *
     * @param groupId 群组Id
     * @throws HttpException
     */
    public GetGroupMemberResponse getGroupMember(String groupId) throws HttpException {
        String url = getURL("group/" + groupId + "/members");
        String result = httpManager.get(mContext, url, mHeadToken);
        GetGroupMemberResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, GetGroupMemberResponse.class);
        }
        return response;
    }

    /**
     * 当前用户添加群组成员
     *
     * @param groupId   群组Id
     * @param memberIds 成员集合
     * @throws HttpException
     */
    public AddGroupMemberResponse addGroupMember(String groupId, List<String> memberIds) throws HttpException {
        String url = getURL("group/add");
        String json = JsonMananger.beanToJson(new AddGroupMemberRequest(groupId, memberIds));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        AddGroupMemberResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, AddGroupMemberResponse.class);
        }
        return response;
    }

    /**
     * 创建者将群组成员提出群组
     *
     * @param groupId   群组Id
     * @param memberIds 成员集合
     * @throws HttpException
     */
    public DeleteGroupMemberResponse deleGroupMember(String groupId, List<String> memberIds) throws HttpException {
        String url = getURL("group/kick");
        String json = JsonMananger.beanToJson(new DeleteGroupMemberRequest(groupId, memberIds));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        DeleteGroupMemberResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, DeleteGroupMemberResponse.class);
        }
        return response;
    }

    /**
     * 创建者更改群组昵称
     *
     * @param groupId 群组Id
     * @param name    群昵称
     * @throws HttpException
     */
    public SetGroupNameResponse setGroupName(String groupId, String name) throws HttpException {
        String url = getURL("group/rename");
        String json = JsonMananger.beanToJson(new SetGroupNameRequest(groupId, name));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        SetGroupNameResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SetGroupNameResponse.class);
        }
        return response;
    }

    /**
     * 用户自行退出群组
     *
     * @param groupId 群组Id
     * @throws HttpException
     */
    public QuitGroupResponse quitGroup(String groupId) throws HttpException {
        String url = getURL("group/quit");
        String json = JsonMananger.beanToJson(new QuitGroupRequest(groupId));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        QuitGroupResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, QuitGroupResponse.class);
        }
        return response;
    }

    /**
     * 创建者解散群组
     *
     * @param groupId 群组Id
     * @throws HttpException
     */
    public DismissGroupResponse dissmissGroup(String groupId) throws HttpException {
        String url = getURL("group/dismiss");
        String json = JsonMananger.beanToJson(new DismissGroupRequest(groupId));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        DismissGroupResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, DismissGroupResponse.class);
        }
        return response;
    }


    /**
     * 修改自己的当前的群昵称
     *
     * @param groupId     群组Id
     * @param displayName 群名片
     * @throws HttpException
     */
    public SetGroupDisplayNameResponse setGroupDisplayName(String groupId, String displayName) throws HttpException {
        String url = getURL("group/set_display_name");
        String json = JsonMananger.beanToJson(new SetGroupDisplayNameRequest(groupId, displayName));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        SetGroupDisplayNameResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SetGroupDisplayNameResponse.class);
        }
        return response;
    }

    /**
     * 删除好友
     *
     * @param friendId 好友Id
     * @throws HttpException
     */
    public DeleteFriendResponse deleteFriend(String friendId) throws HttpException {
        String url = getURL("friendship/delete");
        String json = JsonMananger.beanToJson(new DeleteFriendRequest(friendId));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        DeleteFriendResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, DeleteFriendResponse.class);
        }
        return response;
    }

    /**
     * 设置好友的备注名称
     *
     * @param friendId    好友Id
     * @param displayName 备注名
     * @throws HttpException
     */
    public SetFriendDisplayNameResponse setFriendDisplayName(String friendId, String displayName) throws HttpException {
        String url = getURL("friendship/set_display_name");
        String json = JsonMananger.beanToJson(new SetFriendDisplayNameRequest(friendId, displayName));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        SetFriendDisplayNameResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SetFriendDisplayNameResponse.class);
        }
        return response;
    }

    /**
     * 获取黑名单
     *
     * @throws HttpException
     */
    public GetBlackListResponse getBlackList() throws HttpException {
        String url = getURL("user/blacklist");
        String result = httpManager.get(mContext, url, mHeadToken);
        GetBlackListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, GetBlackListResponse.class);
        }
        return response;
    }

    /**
     * 加入黑名单
     *
     * @param friendId 群组Id
     * @throws HttpException
     */
    public AddToBlackListResponse addToBlackList(String friendId) throws HttpException {
        String url = getURL("user/add_to_blacklist");
        String json = JsonMananger.beanToJson(new AddToBlackListRequest(friendId));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        AddToBlackListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, AddToBlackListResponse.class);
        }
        return response;
    }

    /**
     * 移除黑名单
     *
     * @param friendId 好友Id
     * @throws HttpException
     */
    public RemoveFromBlackListResponse removeFromBlackList(String friendId) throws HttpException {
        String url = getURL("user/remove_from_blacklist");
        String json = JsonMananger.beanToJson(new RemoveFromBlacklistRequest(friendId));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        RemoveFromBlackListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, RemoveFromBlackListResponse.class);
        }
        return response;
    }

    public QiNiuTokenResponse getQiNiuToken() throws HttpException {
        String url = getURL("user/get_image_token");
        String result = httpManager.get(mContext, url, mHeadToken);
        QiNiuTokenResponse q = null;
        if (!TextUtils.isEmpty(result)) {
            q = jsonToBean(result, QiNiuTokenResponse.class);
        }
        return q;
    }


    /**
     * 当前用户加入某群组
     *
     * @param groupId 群组Id
     * @throws HttpException
     */
    public JoinGroupResponse JoinGroup(String groupId) throws HttpException {
        String url = getURL("group/join");
        String json = JsonMananger.beanToJson(new JoinGroupRequest(groupId));
        StringEntity entity = null;
        try {
            entity = new StringEntity(json, ENCODING);
            entity.setContentType(CONTENT_TYPE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = httpManager.post(mContext, url, entity, mHeadToken, CONTENT_TYPE);
        JoinGroupResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, JoinGroupResponse.class);
        }
        return response;
    }


    /**
     * 获取默认群组 和 聊天室
     *
     * @throws HttpException
     */
    public DefaultConversationResponse getDefaultConversation() throws HttpException {
        String url = getURL("misc/demo_square");
        String result = httpManager.get(mContext, url, mHeadToken);
        DefaultConversationResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, DefaultConversationResponse.class);
        }
        return response;
    }

    /**
     * 根据一组ids 获取 一组用户信息
     *
     * @param ids 用户 id 集合
     * @throws HttpException
     */
    public GetUserInfosResponse getUserInfos(List<String> ids) throws HttpException {
        String url = getURL("user/batch?");
        StringBuilder sb = new StringBuilder();
        for (String s : ids) {
            sb.append("id=");
            sb.append(s);
            sb.append("&");
        }
        String stringRequest = sb.substring(0, sb.length() - 1);
        String newUrl = url + stringRequest;
        String result = httpManager.get(mContext, newUrl, mHeadToken);
        GetUserInfosResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, GetUserInfosResponse.class);
        }
        return response;
    }

    /**
     * 获取版本信息
     *
     * @throws HttpException
     */
    public VersionResponse getSealTalkVersion() throws HttpException {
        String url = getURL("misc/client_version");
        String result = httpManager.get(mContext, url.trim(), mHeadToken);
        VersionResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, VersionResponse.class);
        }
        return response;
    }

    public SyncTotalDataResponse syncTotalData(String version) throws HttpException {
        String url = getURL("user/sync/" + version);
        String result = httpManager.get(mContext, url, mHeadToken);
        SyncTotalDataResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SyncTotalDataResponse.class);
        }
        return response;
    }

//    /**
//     * 根据userId去服务器查询好友信息
//     *
//     * @throws HttpException
//     */
//    public GetFriendInfoByIDResponse getFriendInfoByID(String userid) throws HttpException {
//        String url = getURL("friendship/" + userid + "/profile");
//        String result = httpManager.get(url);
//        GetFriendInfoByIDResponse response = null;
//        if (!TextUtils.isEmpty(result)) {
//            response = jsonToBean(result, GetFriendInfoByIDResponse.class);
//        }
//        return response;
//    }
    /**
     //     * 根据userId去服务器查询好友信息
     //     *
     //     * @throws HttpException
     //     */



    /*##########################朋友圈相关##########################*/

    /**
     * 朋友圈动态（分页）
     * 请求类型：post/get
     *
     * @param pageNum  pageNum当前页数（默认为1）
     * @param pageSize pageSize每页显示条数（默认5）
     * @throws HttpException
     */
    public MomentsInfo getListFriendByPage(int pageNum, int pageSize) throws HttpException {
        String url = getURL("trends/listFriendByPage");
        if (pageNum <= 0)
            pageNum = 1;
        RequestParams params = new RequestParams();
        params.put("pageNum", pageNum + "");
        params.put("pageSize", 20 + "");
        String result = httpManager.get(url, params, mHeadToken);
        MomentsInfo response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, MomentsInfo.class);
        }
        return response;
    }

    /**
     * 朋友圈动态评论
     * 请求类型：post/get
     *
     * @param trendsid  动态详情id
     * @param commentid 评论的id(可为空)
     * @param content   评论内容
     * @throws HttpException
     */
    public MomentsInfo addComment(String trendsid, String commentid, String content) throws HttpException {
        String url = getURL("trends/addComment");
        RequestParams params = new RequestParams();
        params.put("trendsid", trendsid);
        params.put("commentid", commentid);
        params.put("content", content);
        String result = httpManager.get(url, params, mHeadToken);
        MomentsInfo response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, MomentsInfo.class);
        }
        return response;
    }

    /**
     * 朋友圈动态评论删除
     * 请求类型：post/get
     *
     * @param ids 为整型数组，可传多个，分割     ids=1,2,3
     * @throws HttpException
     */
    public MomentsInfo deleteComment(ArrayList<String> ids) throws HttpException {
        String url = getURL("trends/deleteComment");
        String listId = null;
        if (ids != null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < ids.size(); i++) {
                sb.append(ids.get(i));
                if (i + 1 < ids.size())
                    sb.append(",");
            }
            listId = sb.toString();
        }
        RequestParams params = new RequestParams();
        params.put("ids", listId);
        String result = httpManager.get(url, params, mHeadToken);
        MomentsInfo response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, MomentsInfo.class);
        }
        return response;
    }

    /**
     * 朋友圈动态点赞/取消点赞
     * 请求类型：post/get
     *
     * @param trendsid 动态详情id
     * @throws HttpException
     */
    public MomentsInfo trendsCommentLike(String trendsid) throws HttpException {
        String url = getURL("trends/trendsCommentLike");
        RequestParams params = new RequestParams();
        params.put("trendsid", trendsid);
        String result = httpManager.get(url, params, mHeadToken);
        MomentsInfo response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, MomentsInfo.class);
        }
        return response;
    }

    /**
     * 朋友圈发布动态（图文格式）content=123&imgId=1,2,3
     * 请求类型：post/get
     *
     * @param content 动态的文本内容
     * @param imgId   上传图片返回的id（可传多个，分割）
     * @throws HttpException
     */
    public MomentsInfo addTrends(String content, String imgId) throws HttpException {
        String url = getURL("trends/addTrends");
        RequestParams params = new RequestParams();
        params.put("content", content);
        params.put("imgId", imgId);
        String result = httpManager.get(url, params, mHeadToken);
        MomentsInfo response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, MomentsInfo.class);
        }
        return response;
    }

    /**
     * 朋友圈删除动态
     * 请求类型：post/get
     *
     * @param ids 为整型数组，可传多个，分割
     * @throws HttpException
     */
    public MomentsInfo deleteTrends(String ids) throws HttpException {
        String url = getURL("trends/deleteTrends");
        RequestParams params = new RequestParams();
        params.put("ids", ids);
        String result = httpManager.get(url, params, mHeadToken);
        MomentsInfo response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, MomentsInfo.class);
        }
        return response;
    }

    /**
     * 删除图片
     * 请求类型：post/get
     *
     * @param ids ids为整型数组，可传多个，分割
     * @throws HttpException
     */
    public MomentsInfo deleteImg(String ids) throws HttpException {
        String url = getURL("trends/deleteImg");
        RequestParams params = new RequestParams();
        params.put("ids", ids);
        String result = httpManager.get(url, params, mHeadToken);
        MomentsInfo response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, MomentsInfo.class);
        }
        return response;
    }


        /*##########################矿机##########################*/

    /**
     * 用户矿机列表（分页）
     * 请求类型：post/get
     * 响应主体属性描述：coinMinute矿机工作时间（分钟）。 计算剩余多少分钟 可根据manhour（小时）* 60 -  coinMinute
     *
     * @param pageNum pageNum当前页数（默认为1），pageSize每页显示条数（默认5）
     * @throws HttpException
     */
    public MachineUserListResponse listMachineUserByPage(String pageNum, String pageSize) throws HttpException {
        String url = getURL("machine/listMachineUserByPage");
        RequestParams params = new RequestParams();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        String result = httpManager.get(url, params, mHeadToken);
        MachineUserListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, MachineUserListResponse.class);
        }
        return response;
    }

    /**
     * 出售的矿机列表
     * 请求类型：post/get
     *
     * @param pageNum pageNum当前页数（默认为1），pageSize每页显示条数（默认5）
     * @throws HttpException
     */
    public SellMachineListResponse sellMachineList(String pageNum, String pageSize) throws HttpException {
        String url = getURL("machine/listByPage");
        RequestParams params = new RequestParams();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        String result = httpManager.get(url, params, mHeadToken);
        SellMachineListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SellMachineListResponse.class);
        }
        return response;
    }

    /**
     * 购买矿机
     * 请求类型：post/get
     *
     * @param machineid 矿机id
     * @throws HttpException
     */
    public SellMachineListResponse machineBuy(String machineid) throws HttpException {
        String url = getURL("machine/buy");
        RequestParams params = new RequestParams();
        params.put("machineid", machineid);
        String result = httpManager.get(url, params, mHeadToken);
        SellMachineListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SellMachineListResponse.class);
        }
        return response;
    }

    /**
     * 开启矿机
     * 请求类型：post/get
     *
     * @param id 用户矿机列表muid
     * @throws HttpException
     */
    public SellMachineListResponse machineOpen(String id) throws HttpException {
        String url = getURL("machine/open");
        RequestParams params = new RequestParams();
        params.put("id", id);
        String result = httpManager.get(url, params, mHeadToken);
        SellMachineListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SellMachineListResponse.class);
        }
        return response;
    }

    /**
     * 关闭矿机
     * 请求类型：post/get
     *
     * @param id 用户矿机列表muid
     * @throws HttpException
     */
    public SellMachineListResponse machineClose(String id) throws HttpException {
        String url = getURL("machine/close");
        RequestParams params = new RequestParams();
        params.put("id", id);
        String result = httpManager.get(url, params, mHeadToken);
        SellMachineListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, SellMachineListResponse.class);
        }
        return response;
    }




    /*##########################用户##########################*/

    /**
     * 当前登录用户详情
     * 请求类型：post/get
     *
     * @throws HttpException
     */
    public UserInfoResponse getUser() throws HttpException {
        String url = getURL("user/getUser");
        RequestParams params = new RequestParams();
        String result = httpManager.get(url, params, mHeadToken);
        UserInfoResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, UserInfoResponse.class);
        }
        return response;
    }



    /*##########################交易##########################*/

    /**
     * 查询货币列表
     * 请求类型：post/get
     *
     * @throws HttpException
     */
    public CoinListResponse coinList() throws HttpException {
        String url = getURL("coin/list");
        RequestParams params = new RequestParams();
        String result = httpManager.get(url, params, mHeadToken);
        CoinListResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, CoinListResponse.class);
        }
        return response;
    }

    /**
     * 查询用户所持有货币数量
     * 请求类型：post/get
     *
     * @param coinid=货币id
     * @throws HttpException
     */
    public CoinCountByUserResponse getCoinCountByUser(String coinid) throws HttpException {
        String url = getURL("coin/getCoinCountByUser");
        RequestParams params = new RequestParams();
        params.put("coinid", coinid);
        String result = httpManager.get(url, params, mHeadToken);
        CoinCountByUserResponse response = null;
        if (!TextUtils.isEmpty(result)) {
            response = jsonToBean(result, CoinCountByUserResponse.class);
        }
        return response;
    }
}
